<?php
	include_once("db/Department.inc");
	include_once("db/AuditLogs.inc");

	$department = new Department();
	$department->id = $_REQUEST["code"];
	$department->description = $_REQUEST["description"];
	
	/*
	 * Check if the case type code already exists.  If so, redirect back to the add department page with
	 * an error.
	 */
	if (Department::getDepartment($department->id) != null && $_REQUEST["oldCode"]=="0") {
		header("Location: ?action=editDepartment&error=deptCodeExists");
	} else {
		Department::updateDepartment($department, $_REQUEST["oldCode"]);
		if ($_REQUEST["oldCode"]=="0") {
			AuditLogs::insertLog(User::getUser($_SESSION["uid"])->username, 'admin - add department',
				"Code='".$department->id." Description='".addslashes($department->description)."'");
		} else {
			AuditLogs::insertLog(User::getUser($_SESSION["uid"])->username, 'admin - modifiy department',
				"Old Code='".$_REQUEST["oldCode"]."' New Code='".$department->id." Description='".
				addslashes($department->description)."'");
		}
		
		header("Location: ./?action=viewDepartments");
	}
?>